# Author: Mark Richardson
# Purpose: Figures of performance ratings for main text

# Load packages
library(dplyr)
library(ggplot2)
library(stringr)
library(patchwork)

# Load data
load("Data/07_performance_ratings_all_fmtd.RData")

#### Figure 1 ####

perf_ratings <- perf_ratings %>%
  arrange(perf_rating_inf_hier) %>%
  mutate(plot_index = 1:n()) %>%
  mutate(agency = if_else(agency == "Bureau of International Narcotics and Law Enforcement Affairs",
                          "Bureau of Int. Narcotics and Law Enf. Affairs",
                          agency))

p1 <- ggplot(perf_ratings %>% filter(plot_index <= 71)) +
  geom_segment(aes(x = perf_rating_inf_hier_lb, y = plot_index,
                   xend = perf_rating_inf_hier_ub, yend = plot_index), alpha = 0.6) +
  geom_point(aes(perf_rating_inf_hier, plot_index), size = 2) +
  geom_vline(xintercept = 0, linetype = 2) +
  geom_text(aes(x = -3.25, y = plot_index, label = agency, hjust = "right")) +
  labs(x = "Latent Performance", y = "") +
  theme_bw() +
  theme(
    axis.text.x = element_text(size = 12),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()) +
  scale_x_continuous(breaks = -3:3, limits = c(-15.75,3.5),
                     sec.axis = dup_axis(name = ""))

p2 <- ggplot(perf_ratings %>% filter(plot_index >= 72) %>% mutate(plot_index = plot_index - 71)) +
  geom_segment(aes(x = perf_rating_inf_hier_lb, y = plot_index,
                   xend = perf_rating_inf_hier_ub, yend = plot_index), alpha = 0.6) +
  geom_point(aes(perf_rating_inf_hier, plot_index), size = 2) +
  geom_vline(xintercept = 0, linetype = 2) +
  geom_text(aes(x = -3.25, y = plot_index, label = agency, hjust = "right")) +
  labs(x = "Latent Performance", y = "") +
  theme_bw() +
  theme(
    axis.text.x = element_text(size = 12),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()) +
  scale_x_continuous(breaks = -3:3, limits = c(-15.75,3.5),
                     sec.axis = dup_axis(name = ""))

p <- p2 + p1 +
  plot_annotation(caption = "Horizontal lines give the 95% Bayesian credible interval. Agencies with at least 5 ratings.")

ggsave("figs/fg1.png",
       plot = p,
       device = "png", dpi = "retina",
       units = "in", width = 12, height = 12)


#### Figure 3 ####

ggplot(perf_ratings, aes(perf_rating_rep, perf_rating_dem)) +
  geom_point(alpha = 0.75) +
  geom_abline() +
  theme_bw() +
  labs(x = "Republican Performance Rating",
       y = "Democratic Performance Rating") +
  xlim(-2.5, 2.5) +
  ylim(-2.5, 2.5)

ggsave("figs/fg3.png",
       device = "png", dpi = "retina",
       units = "in",  width = 7, height = 5)

cor(perf_ratings$perf_rating_rep, perf_ratings$perf_rating_dem,
    use = "complete")

#### Figure 4 ####

ggplot(perf_ratings %>% arrange(perf_rating_dif) %>% filter(!is.na(perf_rating_dif)) %>% mutate(plot_index = 1:n())) +
  geom_segment(aes(x = perf_rating_dif_lb_90, y = plot_index, xend = perf_rating_dif_ub_90, yend = plot_index), alpha = 0.6) +
  geom_point(aes(perf_rating_dif, plot_index), size = 2) +
  geom_vline(xintercept = 0, linetype = 2) +
  geom_text(aes(x = -4, y = plot_index, label = agency, hjust = "right")) +
  labs(x = "Democrat rating less Republican rating", y = "",
       caption = "Horizontal lines give the 90% Bayesian credible interval. Agencies with at least 5 ratings from each party.") +
  annotate(geom = "text", x = c(2.5, -3), y = c(35, 35),
           label = c("Democrats perceive better\nperformance than Republicans", "Republicans perceive better\nperformance than Democrats."),
           hjust = 0) +
  theme_bw() +
  theme(
    axis.text.x = element_text(size = 12),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()) +
  scale_x_continuous(breaks = -4:4, limits = c(-6,4),
                     sec.axis = dup_axis(name = ""))

ggsave("figs/fg4.png",
       device = "png", dpi = "retina",
       units = "in", width = 16, height = 8)

